package com.blankland.dataanalysis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blankland.dataanalysis.domain.Device;
import com.blankland.dataanalysis.domain.GammaData;
import com.blankland.dataanalysis.result.Result;
import com.blankland.dataanalysis.service.DeviceService;
import com.blankland.dataanalysis.service.GammaDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "伽马设备管理接口")
@RestController
@RequestMapping("/admin/system/device")
public class DeviceController {


    @Autowired
    private DeviceService deviceService;



    @ApiOperation("查询设备：所有")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Device> list = deviceService.list();
        return Result.ok(list);
    }

    @ApiOperation("查询设备：根据id查询")
    @GetMapping("/findDeviceById/{id}")
    public Result findDeviceById(@PathVariable Long id){
        Device device = deviceService.getById(id);
        return Result.ok(device);
    }

    @ApiOperation("修改设备：为设备添加参数")
    @PutMapping("/update")
    public Result updateDeviceParams(@RequestBody Device device){
        //1、根据设备中K40的能量、K40的道址、Ti208的能量、Ti208的道值，计算出对应的b和w
        Double k40Energy = device.getK40Energy();
        Integer k40Channel = device.getK40Channel();
        Double ti208Energy = device.getTi208Energy();
        Integer ti208Channel = device.getTi208Channel();
        double k = (ti208Energy-k40Energy)/(ti208Channel-k40Channel);
        double b = k40Energy-k*k40Channel;

        //2、将k和b添加到device对象中
        device.setK(k);
        device.setB(b);


        //3、更新参数
        boolean flag = deviceService.updateById(device);
        if (true == flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("分页查询")
    @GetMapping("/findDeviceByPage")
    public Result findDeviceByPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize){

        Page<Device> devicePage = new Page<>(pageNum,pageSize);
        Page<Device> page = deviceService.page(devicePage);
        return Result.ok(page);
    }


    @ApiOperation("分页查询+模糊查询")
    @GetMapping("/findDeviceByPageAndCondition")
    public Result findDevicceByPageAndCondtion(@RequestParam Integer pageNum,
                                               @RequestParam Integer pageSize,
                                               @RequestParam(defaultValue = "") String deviceName){
        Page<Device> devicePage = new Page<>(pageNum, pageSize);

        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.like("device_name",deviceName);
        wrapper.orderByDesc("device_id");
        Page<Device> page = deviceService.page(devicePage, wrapper);
        return Result.ok(page);
    }


}
